package cn.MaySoon.zhuansheng;

import net.milkbowl.vault.economy.Economy;
import org.black_ixx.playerpoints.PlayerPoints;
import org.black_ixx.playerpoints.PlayerPointsAPI;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.serverct.ersha.attribute.data.AttributeSource;

import java.io.File;
import java.util.HashMap;

public class main extends JavaPlugin {

    public static main plugin;
    static FileConfiguration config;
    static File filess;
    public static PlayerPoints playerPoints;
    public Economy economy = null;
    public boolean isEco = false;
       public static HashMap<Player, zhuansheng> PlayerZhaunSheng = new HashMap<Player, zhuansheng>();
        PlayerPointsAPI ppai ;
       public static HashMap<Player, AttributeSource> PlayerAttribute = new HashMap<Player, AttributeSource>();
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(new listen(),this);

            plugin = this;
            config = getConfig();
            filess = new File(getDataFolder(), "config.yml");
            this.isEco = onEconomy();
            saveDefaultConfig();
            new PAPIHooker(this).hook();
            hookPlayerPoints();
        }

    private boolean hookPlayerPoints() {
        final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("PlayerPoints");
        playerPoints = PlayerPoints.class.cast(plugin);
        return playerPoints != null;
    }
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    {
        if (label.equalsIgnoreCase("zhuansheng")){
            if (args[0].equalsIgnoreCase("give")){
                if (sender.hasPermission("zhuansheng.admin")) {
                    Player p = Bukkit.getPlayer(args[1]);
                    int add = Integer.parseInt(args[2]);
                    handle.updatePlayerXiuwei(p,add);
                    sender.sendMessage("ok");
                    return true;
                }
                else{
                    return true;
                }
            }
                if (args[0].equalsIgnoreCase("up")){
                    Player p = (Player) sender;
                    handle.updatePlayerLevel(p);
                    return true;
                }
            if (args[0].equalsIgnoreCase("usexiaoxiuwei")){
                Player p = (Player) sender;
                handle.usexiaoxiuwei(p);
                return true;
            }
            if (args[0].equalsIgnoreCase("buyxiuwei")){
                Player p = (Player) sender;
                handle.buyxiuweidan(p);
                return true;
            }
            if (args[0].equalsIgnoreCase("buydaxiuwei")){
                Player p = (Player) sender;
                handle.buydaxiuweidan(p);
                return true;
            }
            if (args[0].equalsIgnoreCase("exchangexiuwei")){
                Player p = (Player) sender;
                handle.exchangexiuwei(p);
                return true;
            }

        }
        return true;
    }

    private boolean onEconomy()
    {

        RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(Economy.class);
        if ((economyProvider != null) && ((this.economy = (Economy)economyProvider.getProvider()) == null)) {
            this.isEco = true;
        }
        return !this.isEco;
    }



}
